package com.zsh.wanandroid.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zsh.wanandroid.R;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.bean.KnowledgeChildren;
import com.zsh.wanandroid.bean.KnowledgeTree;
import com.zsh.wanandroid.ui.fragment.CommonPagerFragment;
import com.zsh.wanandroid.ui.fragment.SystemListPagerFragment;
import com.zsh.wanandroid.utils.UIUtils;

import java.util.List;

/**
 * @author zsh
 * @date 2020/3/20.
 * 功能：体系列表页面
 */

public class SystemListActivity extends BaseActivity {

    private ImageView ivBack;
    private TextView tvTitle;
    private ImageView ivMore;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private KnowledgeTree<KnowledgeChildren> data;

    private SparseArray<SystemListPagerFragment> tabFragmentSparseArray = new SparseArray<>();

    @Override
    public void initPrams(Bundle prams) {
        if (null != prams)
            data = (KnowledgeTree<KnowledgeChildren>) prams.getSerializable("data");
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_system_list;
    }

    @Override
    public void initView(View view) {
        ivBack = $(R.id.ivBack);
        tvTitle = $(R.id.tvTitle);
        ivMore = $(R.id.ivMore);
        tabLayout = $(R.id.tabLayout);
        viewPager = $(R.id.viewPager);
        swipeRefreshLayout = $(R.id.swipeRefreshLayout);
        recyclerView = $(R.id.recyclerView);
    }

    @Override
    public void setListener() {
        ivBack.setOnClickListener(this);
        ivMore.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack://返回
                back();
                break;

            case R.id.ivMore://更多
                UIUtils.showToast("更多");
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        //设置标题
        tvTitle.setText(data.getName());
        ivMore.setImageResource(R.drawable.ic_more_white_24dp);
        //初始化设置tabLayout
        initTabLayout(data);
    }

    private void initTabLayout(KnowledgeTree<KnowledgeChildren> data) {
        List<KnowledgeChildren> children = data.getChildren();
        for (int i = 0; i < children.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(children.get(i).getName()));
        }

        initViewPager(children);

        //tabLayout监听事件
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //添加选中Tab的逻辑
                viewPager.setCurrentItem(tab.getPosition(), false);//改变viewpager
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //再次选中tab的逻辑
            }
        });
    }

    private void initViewPager(final List<KnowledgeChildren> children) {
        //viewPager设置adapter
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("cid", children.get(position).getId());
                bundle.putInt("default_page", 0);
                return new SystemListPagerFragment().newInstance(bundle);
            }

            @Override
            public int getCount() {
                return children.size();
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                SystemListPagerFragment basePF = (SystemListPagerFragment) super.instantiateItem(container, position);
                tabFragmentSparseArray.put(position, basePF);
                return basePF;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                //tabFragmentSparseArray.remove(position);
                //super.destroyItem(container, position, object);
            }
        });

        //viewPager滑动监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            /**
             * 当某个页面被选中的时候回调这个方法
             * @param position 被选中页面的位置
             */
            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void back() {
        this.finish();
    }

    @Override
    public void onBackPressed() {
        back();
    }
}
